package com.buss.FrontController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.EncryptUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.MutiLangServiceI;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.integral.entity.BnsIntegralEntity;
import com.buss.integral.service.BnsIntegralServiceI;
import com.buss.user.entity.BnsUsersInfoEntity;
import com.buss.user.entity.ShieldingMessageEntity;
import com.buss.user.entity.UserDynamicSetEntity;
import com.buss.user.entity.UserPrivacySet;
import com.buss.user.service.BnsUsersInfoServiceI;

/**
 * 用户信息控制层
 * @author hlg
 * @Date 2016-08-17
 */
@Scope("prototype")
@Controller
@RequestMapping("/userInfoController")
public class UserInfoController extends BaseController {
	private static final Logger logger = Logger.getLogger(UserInfoController.class);

	private String message;
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private BnsUsersInfoServiceI bnsUsersInfoServiceI;
	
	@Autowired
	private MutiLangServiceI mutiLangService;
	
	@Autowired
	private BnsIntegralServiceI integralService;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	
	/**
	 * 修改头像页面
	 * @return
	 */
	@RequestMapping(params = "modifyPage")
	public ModelAndView modifyPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		String options = request.getParameter("options"); // 选项：基本资料、联系方式、教育情况、工作情况、个人信息
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/modifyPage";

		
		model.addAttribute("setPageType", "modifyPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 用户信息页面
	 * @return
	 */
	@RequestMapping(params = "userInfosPage")
	public ModelAndView UserInfosPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		String options = request.getParameter("options"); // 选项：基本资料、联系方式、教育情况、工作情况、个人信息
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		userInfo = systemService.findUniqueByProperty(BnsUsersInfoEntity.class, "userId", userInfo.getUserId());
		String pathStr = "front/userInfos";
		model.addAttribute("options", options);
		model.addAttribute("userInfo", userInfo);
		model.addAttribute("setPageType", "userInfosPage");
		return new ModelAndView(pathStr);
	}
	
	

	/**
	 * 保存用户信息
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "saveUserInfos")
	@ResponseBody
	public AjaxJson SaveUserInfos(HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		String options = request.getParameter("options"); // 选项：基本资料、联系方式、教育情况、工作情况、个人信息
		
		userInfo = bnsUsersInfoServiceI.findUniqueByProperty(BnsUsersInfoEntity.class, "userId", userInfo.getUserId());
		
		AjaxJson j = new AjaxJson();
		
		if(options.equals("base")){	//基本资料
			String realname = request.getParameter("realname");
			String birthyear = request.getParameter("birthyear");	//生日
			String birthmonth = request.getParameter("birthmonth");	//生日
			String birthday = request.getParameter("birthday");	//生日
			String birthprovince = request.getParameter("birthprovince");	//出生地
			String bloodtype = request.getParameter("bloodtype");	//血型
			String lookingfor = request.getParameter("lookingfor");	//交友目的
			String resideprovince = request.getParameter("resideprovince");	//居住地
			String affectivestatus = request.getParameter("affectivestatus");	//情感状态
			
			String birthdays = birthyear + "-" + birthmonth + "-" + birthday;
			
			if(StringUtil.isEmpty(realname)){
				j.setMsg("真实姓名不能为空!");
				j.setSuccess(false);
			}else{
				userInfo.setRealName(realname);
				if(StringUtil.isNotEmpty(birthdays)){
					userInfo.setBirthday(birthdays);
				}
				userInfo.setAddress(birthprovince);
				userInfo.setBloodType(bloodtype);
				userInfo.setPurpose(lookingfor);
				userInfo.setAbode(resideprovince); 
				userInfo.setEmotionalStates(affectivestatus);
//				bnsUsersInfoServiceI.updateEntitie(userInfo); 
			}
			
		}
		
		if(options.equals("contact")){	//联系方式
			String qq = request.getParameter("qq");	//qq
			String wechat = request.getParameter("wechat");	//msn
			String mobile = request.getParameter("mobile");	//手机号码
			
			if(StringUtil.isEmpty(mobile)){
				j.setMsg("电话不能为空!");
				j.setSuccess(false);
			}else{
				userInfo.setQq(qq);
				userInfo.setWechat(wechat);
				userInfo.setMobile(mobile);
//				bnsUsersInfoServiceI.updateEntitie(userInfo); 
			}
			
		}
		
		if(options.equals("edu")){	//教育情况
			String education = request.getParameter("education");	//学历
			String graduateschool = request.getParameter("graduateschool");	//毕业学校
			userInfo.setEducation(education); 
			userInfo.setGraduationSchool(graduateschool);
//			bnsUsersInfoServiceI.updateEntitie(userInfo); 
		}
		
		if(options.equals("work")){	//工作情况
			String company = request.getParameter("company");	//公司
			String revenue = request.getParameter("revenue");	//年收入
			String position = request.getParameter("position");	//职位
			String occupation = request.getParameter("occupation");	//职业
			userInfo.setCompany(company);
			userInfo.setAnnualIncome(revenue);
			userInfo.setPosition(position);
			userInfo.setProfession(occupation);
//			bnsUsersInfoServiceI.updateEntitie(userInfo); 
		}
		
		if(options.equals("info")){	//个人信息
			String introduction = request.getParameter("introduction");	//自我介绍
			String site = request.getParameter("site");	//个人主页
			String idcard = request.getParameter("idcard");	//证件号
			String address = request.getParameter("address");	//邮寄地址
			String zipcode = request.getParameter("zipcode");	//邮编
			String interest = request.getParameter("interest");	//兴趣爱好
			String idcardtype = request.getParameter("idcardtype");	//证件类型
			
			if(StringUtil.isEmpty(idcard)){
				j.setMsg("证件号不能为空!");
				j.setSuccess(false);
			}else if(StringUtil.isEmpty(idcardtype)){
				j.setMsg("证件类型不能为空!");
				j.setSuccess(false);
			}else{
				userInfo.setSelfIntroduction(introduction);
				userInfo.setPersonalHomepage(site);
				userInfo.setIdNo(idcard);
				userInfo.setAbode(address);
				userInfo.setPostcode(zipcode);
				userInfo.setHobbies(interest);
				userInfo.setIdType(idcardtype);
			}
			
		}
		userInfo.setModifyTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		bnsUsersInfoServiceI.updateEntitie(userInfo); 
		
		return j;
	}

	/**
	 * 手机认证页面
	 * @return
	 */
	@RequestMapping(params = "phoneAuthPage")
	public ModelAndView phone (Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/phoneAuthPage";
		if(StringUtil.isNotEmpty(userInfo.getMobile())){
			//电话已经绑定的 显示 转字符串的号码
			String phoneStr = userInfo.getMobile();
			String phoneViewStr = phoneStr.substring(0,phoneStr.length()-(phoneStr.substring(3)).length())+"****"+phoneStr.substring(7);
			model.addAttribute("phoneViewStr", phoneViewStr);
		}
		model.addAttribute("userInfo", userInfo);
		model.addAttribute("setPageType", "phoneAuthPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * TODO 发送验证码
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "sendAuthCode")
	@ResponseBody
	public AjaxJson sendAuthCode(HttpServletRequest request) {
		HttpSession session = request.getSession();
		AjaxJson j = new AjaxJson();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			j.setMsg("请登录!");
			j.setSuccess(false);
			return j;
		}
		String phoneNumber = request.getParameter("phone");	//电话号码
		return j;
	}
	
	/**
	 * 保存电话号码
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "saveAuthPhone")
	@ResponseBody
	public AjaxJson saveAuthPhone(HttpServletRequest request) {
		HttpSession session = request.getSession();
		AjaxJson j = new AjaxJson();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			j.setMsg("请登录!");
			j.setSuccess(false);
			return j;
		}
		userInfo = systemService.findUniqueByProperty(BnsUsersInfoEntity.class, "userId", userInfo.getUserId());
		String phoneNumber = request.getParameter("phone");	//电话号码
		
		if(StringUtil.isEmpty(phoneNumber)){
			j.setMsg("请输入电话号码!");
			j.setSuccess(false);
			return j;
		}
		if(StringUtil.isEmpty(userInfo.getMobile())){
			userInfo.setMobile(phoneNumber);
			systemService.save(userInfo);
			j.setMsg("保存成功!");
			j.setSuccess(true);
		}
		return j;
	}
	
	/**
	 * 积分页面
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(params = "Integral")
	public ModelAndView Integral(Model model,HttpServletRequest request) throws ParseException {
		String pathStr = "front/integral";
		//获取会员id  --zyb
		Object mbId = request.getSession().getAttribute("mbId");
//		System.out.print("--------------mbId:"+mbId);
		String loginMember = (String) request.getSession().getAttribute("loginMember");
		
		//判断登录是否超时  --zyb
		if(StringUtil.isEmpty(loginMember) && StringUtil.isEmpty(mbId.toString())){	
			loginMember = "";
			pathStr = "front/login";	
		}else{
			HttpSession session = request.getSession();
			String options = request.getParameter("options"); // 选项：积分、充值、转账、兑换、记录、规则  --zyb
			BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
			model.addAttribute("options", options);
			model.addAttribute("userInfo", userInfo);
			
			//查询相应会员  --zyb
			List<Map<String, Object>> myUser = systemService.findForJdbc("SELECT * FROM bns_users_info WHERE user_id = '"+mbId+"'");
			if(myUser!=null && myUser.size()>0){
				request.setAttribute("myUser", myUser.get(0));
//				uPw = myUser.get(0).get("passWord").toString();
			}else{
				request.setAttribute("myUser","");
			}
			
			//############ 积分记录开始
				//积分记录 --zyb
				String jflx = request.getParameter("jflx");
				String sj1 = request.getParameter("sj1");
				String sj2 = request.getParameter("sj2");
				String sz = request.getParameter("sz");
				String jfcz = request.getParameter("jfcz");
				//批量删除
				String  jlId[]    =  request.getParameterValues("ckb"); 
//				String  jlIdAll  =  "";
				if(jlId != null){
					 for(int  i=0;i<jlId.length;i++){ 
//						 BnsIntegralEntity myInte = new BnsIntegralEntity();
//						 myInte = integralService.getEntity(BnsIntegralEntity.class, jlId[i]);
						 integralService.deleteEntityById(BnsIntegralEntity.class, jlId[i]);
						 
					 }      	
				}
				
				//string转date的类  --zyb
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
				
				//前台点击的页码  --zyb
				String page=request.getParameter("page");
				int pageParam=0;
				if(StringUtil.isEmpty(page))
					pageParam=0;
				else
					pageParam=Integer.parseInt(page);

				Long dataCount=0L;	//数据总数  --zyb
				
				CriteriaQuery cq = new CriteriaQuery(BnsIntegralEntity.class);
				dataCount = systemService.getCountForJdbcParam("select count(1) from bns_integral",null);
				
				cq.eq("userId",Integer.parseInt(String.valueOf(mbId)));
				if(StringUtil.isNotEmpty(jflx)){
					cq.eq("integralType", jflx);
				}
				if(StringUtil.isNotEmpty(sj1)){
					Date date1 = sdf.parse(sj1);
					cq.ge("addDate", date1);
				}
				if(StringUtil.isNotEmpty(sj2)){
					Date date2 = sdf.parse(sj2);
					cq.le("addDate", date2);
				}
				if(StringUtil.isNotEmpty(sz)){
					cq.eq("inOut", sz);
				}
				if(StringUtil.isNotEmpty(jfcz)){
					cq.eq("addType", jfcz);
				}
				
				//分页
				int myPageSize = 5;
				if("wdjf".equals(options)){
					myPageSize = 4;	//我的积分页面的记录 每页4个  --zyb
				}
				cq.setPageSize(myPageSize);
				cq.setCurPage(pageParam);
					
				cq.add();
				
				List<BnsIntegralEntity> JfjlList = systemService.getListByCriteriaQuery(cq, true);
				//计算总页数  --zyb
				int pageCount=0;
				if(dataCount.intValue()>0 && dataCount.intValue()%myPageSize==0)
					pageCount=dataCount.intValue()/myPageSize;
				else
					pageCount=dataCount.intValue()/myPageSize+1;
				
				request.setAttribute("pageCount",pageCount); 
				request.setAttribute("JfjlList", JfjlList);
			//########## 积分记录结束
				
			if("jfgz".equals(options)){
				//积分规则 --zyb
				List<Map<String, Object>> JfgzList = systemService.findForJdbc("SELECT * FROM bns_integral_rule ORDER BY sort ASC");
				if(JfgzList != null && JfgzList.size() > 0){
					request.setAttribute("JfgzList", JfgzList);
				}else{
					request.setAttribute("JfgzList", "");
				}	
			}			
		}
				
		model.addAttribute("setPageType", "Integral");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 用户组页面
	 * @return
	 */
	@RequestMapping(params = "userGroupPage")
	public ModelAndView userGroupPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/userGroupPage";
		
		model.addAttribute("setPageType", "userGroupPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 购买用户组页面
	 * @return
	 */
	@RequestMapping(params = "buyUserGroupPage")
	public ModelAndView buyUserGroupPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/buyUserGroupPage";
		
		model.addAttribute("setPageType", "buyUserGroupPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 我的论坛权限页面
	 * @return
	 */
	@RequestMapping(params = "myForumPermissionsPage")
	public ModelAndView myForumPermissionsPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/myForumPermissionsPage";
		
		model.addAttribute("setPageType", "myForumPermissionsPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 隐私筛选页面
	 * @return
	 */
	@RequestMapping(params = "privacyScreeningPage")
	public ModelAndView privacyScreeningPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/privacyScreeningPage";
		String options = request.getParameter("options"); // 选项：个人隐私设置 个人动态发布设置 动态筛选 
		UserPrivacySet userPrivacySet= systemService.findUniqueByProperty(UserPrivacySet.class, "userId", userInfo.getUserId());
		UserDynamicSetEntity userDynamicSet= systemService.findUniqueByProperty(UserDynamicSetEntity.class, "userId", userInfo.getUserId());
		ShieldingMessageEntity shieldingEntity =  systemService.findUniqueByProperty(ShieldingMessageEntity.class, "userId", userInfo.getUserId());

		model.addAttribute("options", options);
		model.addAttribute("userPrivacySet", userPrivacySet);
		model.addAttribute("userDynamicSet", userDynamicSet);
		model.addAttribute("shieldingEntity", shieldingEntity);
		model.addAttribute("setPageType", "privacyScreeningPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 保存用户隐私设置
	 * 设置：0-公开、1-好友可见、2-保密、3-只有注册用户可见
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "saveUserPrivacys")
	@ResponseBody
	public AjaxJson SaveUserPrivacys(HttpServletRequest request){
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		AjaxJson j = new AjaxJson();
		String options = request.getParameter("options");
		
		UserPrivacySet userPrivacySet= systemService.findUniqueByProperty(UserPrivacySet.class, "userId", userInfo.getUserId());
		
		if (!StringUtil.isNotEmpty(userInfo)) {
			j.setMsg("请重新登录!");
			j.setSuccess(false); 
			return j;
		}
		
		if (options.equals("privacy")) {
			String hyValue = request.getParameter("hyValue");
			String lybValue = request.getParameter("lybValue");
			String dtValue = request.getParameter("dtValue");
			String jlValue = request.getParameter("jlValue");
			String rzValue = request.getParameter("rzValue");
			String xcValue = request.getParameter("xcValue");
			String fxValue = request.getParameter("fxValue");
			
			userPrivacySet.setUserId(userInfo.getUserId()); 
			userPrivacySet.setFriendList(Integer.valueOf(hyValue));
			userPrivacySet.setMessageBoard(Integer.valueOf(lybValue));
			userPrivacySet.setDynamicState(Integer.valueOf(dtValue));
			userPrivacySet.setRecord(Integer.valueOf(jlValue));
			userPrivacySet.setLog(Integer.valueOf(rzValue));
			userPrivacySet.setPhotoAlbum(Integer.valueOf(xcValue));
			userPrivacySet.setShare(Integer.valueOf(fxValue));
			userPrivacySet.setUpdateTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		
			systemService.updateEntitie(userPrivacySet);
		}
		
		if(options.equals("dynamic")){	
			UserDynamicSetEntity userDynamicSet= systemService.findUniqueByProperty(UserDynamicSetEntity.class, "userId", userInfo.getUserId());
			String jl = request.getParameter("jl");
			String rz = request.getParameter("rz");
			String img = request.getParameter("img");
			String fx = request.getParameter("fx");
			String hy = request.getParameter("hy");
			String pl = request.getParameter("pl");
			String jjpm = request.getParameter("jjpm");	//竞价排名
			String jfxf = request.getParameter("jfxf");
			String yqhy = request.getParameter("yqhy");
			String wcrw = request.getParameter("wcrw");
			String grzl = request.getParameter("grzl");	//更新个人资料
			String bt = request.getParameter("bt");
			String rtft = request.getParameter("rtft");
			String rtht = request.getParameter("rtht");
			
			userDynamicSet.setUserId(userInfo.getUserId()); 
			userDynamicSet.setRecord(Integer.valueOf(jl));
			userDynamicSet.setLog(Integer.valueOf(rz));
			userDynamicSet.setUploadImg(Integer.valueOf(img));
			userDynamicSet.setAddShare(Integer.valueOf(fx));
			userDynamicSet.setAddFirend(Integer.valueOf(hy));
			userDynamicSet.setComment(Integer.valueOf(pl));
			userDynamicSet.setPaidListing(Integer.valueOf(jjpm));
			userDynamicSet.setPointConsumption(Integer.valueOf(jfxf));
			userDynamicSet.setInviteFriends(Integer.valueOf(yqhy));
			userDynamicSet.setCompletionTask(Integer.valueOf(wcrw));
			userDynamicSet.setUpdateInfor(Integer.valueOf(grzl));
			userDynamicSet.setRzDeclare(Integer.valueOf(bt));
			userDynamicSet.setForumFt(Integer.valueOf(rtft));
			userDynamicSet.setForumHt(Integer.valueOf(rtht));
			userDynamicSet.setUpdateTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss")); 
			systemService.updateEntitie(userDynamicSet);
			
		}
		
		if(options.equals("Filtrate")){	
			String qt = request.getParameter("qt");
			String bz = request.getParameter("bz");
			String hd = request.getParameter("hd");
			String py = request.getParameter("py");
			String qr = request.getParameter("qr");
			String ts = request.getParameter("ts");
			String tx = request.getParameter("tx");
			String brs = request.getParameter("brs");
			ShieldingMessageEntity shieldingEntity =  systemService.findUniqueByProperty(ShieldingMessageEntity.class, "userId", userInfo.getUserId());
			
			if(!StringUtil.isNotEmpty(shieldingEntity)){	// 为空
				shieldingEntity = new ShieldingMessageEntity();
				shieldingEntity.setSetTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss")); 
			}else{
				shieldingEntity.setUpTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss")); 
				
			}
			shieldingEntity.setUserId(userInfo.getUserId()); 
			shieldingEntity.setOther(Integer.valueOf(qt));
			shieldingEntity.setSite(Integer.valueOf(bz));
			shieldingEntity.setActivity(Integer.valueOf(hd));
			shieldingEntity.setFirend(Integer.valueOf(py));
			shieldingEntity.setFamily(Integer.valueOf(qr));
			shieldingEntity.setColleagues(Integer.valueOf(ts));
			shieldingEntity.setClassmates(Integer.valueOf(tx));
			shieldingEntity.setNoKnow(Integer.valueOf(brs));
			
			if(!StringUtil.isNotEmpty(shieldingEntity)){	// 为空
				systemService.updateEntitie(shieldingEntity);
			}else{
				systemService.save(shieldingEntity);
			}
			
		}
		
		
		return j;
	}
	
	/**
	 * 密码安全页面
	 * @return
	 */
	@RequestMapping(params = "modifyPassWordPage")
	public ModelAndView ModifyPassWordPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/passWordSecurity";
		model.addAttribute("userInfo", userInfo);
		model.addAttribute("setPageType", "passWordSecurityPage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * 修改密码
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "modifyPassWord")
	@ResponseBody
	public AjaxJson ModifyPassWord(HttpServletRequest request) {
		HttpSession session = request.getSession();
		AjaxJson j = new AjaxJson();
		
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		int userId = userInfo.getUserId();
		
		String oldPassWord = request.getParameter("oldPassWord");
		String newPassWord = request.getParameter("newPassWord");
		String qrPassWord = request.getParameter("qrPassWord");
		String question = request.getParameter("question");	//问题
		String answer = request.getParameter("answer");	//回答
		String randCodeImageStr = request.getParameter("randCodeImageStr"); // 网页验证码
		
//		BnsUsersInfoEntity bnsUsersInfoEntity = bnsUsersInfoServiceI.findSingleResult(userId); 
		
		BnsUsersInfoEntity bnsUsersInfoEntity = systemService.findUniqueByProperty(BnsUsersInfoEntity.class, "userId", userInfo.getUserId());
		
		if (!StringUtil.isNotEmpty(userId)) {
			j.setMsg("请登录!");
			j.setSuccess(false);
			return j;
		}
		
		if (StringUtil.isEmpty(randCodeImageStr)) {
			j.setMsg("请输入验证码!");
			j.setSuccess(false);
			return j;
		}
		
		if (request.getParameter("langCode") != null) {
			request.getSession().setAttribute("lang", request.getParameter("langCode"));
		}
		
		if (bnsUsersInfoEntity != null) {
			String password = bnsUsersInfoEntity.getPassWord();
			oldPassWord = EncryptUtil.encrypt(oldPassWord, "MD5");
			if(!oldPassWord.equals(password)){
				j.setMsg("输入的密码不对!");
				j.setSuccess(false);
				return j;
			}
		}
		
		if (!newPassWord.equals(qrPassWord)) {
			j.setMsg("2次输入的新密码不一样!");
			j.setSuccess(false);
			return j;
		}
		
		String ask = bnsUsersInfoEntity.getAsk();
		if(!StringUtil.isEmpty(ask)){
			if(question.equals(ask)){
				j.setMsg("回答的问题不对!");
				j.setSuccess(false);
				return j;
			}
			
			if(!answer.equals(bnsUsersInfoEntity.getAns())){
				j.setMsg("回答的问题不对!");
				j.setSuccess(false);
				return j;
			}
		}
		
		if (StringUtils.isEmpty(randCodeImageStr)) {
			j.setMsg(mutiLangService.getLang("common.enter.verifycode"));
			j.setSuccess(false);
			return j;
		}else if (!randCodeImageStr.equalsIgnoreCase(String.valueOf(session.getAttribute("randCode")))) {
			j.setMsg(mutiLangService.getLang("common.verifycode.error"));
			j.setSuccess(false);
			return j;
		}
		
		newPassWord = EncryptUtil.encrypt(newPassWord, "MD5");
		bnsUsersInfoEntity.setPassWord(newPassWord);
		bnsUsersInfoServiceI.updateEntitie(bnsUsersInfoEntity);
		
		j.setMsg("修改成功");
		j.setSuccess(true);
		
		return j;
	}
	
	/**
	 * 密码修改成功
	 * @return
	 */
	@RequestMapping(params = "modifySucceed")
	public ModelAndView ModifySucceed(Model model,HttpServletRequest request) {
		return new ModelAndView("redirect:/indexController.do?index"); //修改成功重定向 到会员中心
	}
	
	
	/**
	 * 访问推广页面
	 * @return
	 */
	@RequestMapping(params = "promotePage")
	public ModelAndView promotePage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/promotePage";
		
		
		model.addAttribute("setPageType", "promotePage");
		return new ModelAndView(pathStr);
	}
	
	/**
	 * QQ绑定页面
	 * @return
	 */
	@RequestMapping(params = "qqBingingPage")
	public ModelAndView qqBingingPage(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		String pathStr = "front/qqBingingPage";
		
		
		model.addAttribute("setPageType", "qqBingingPage");
		return new ModelAndView(pathStr);
	}
	
	
	
	
	/**
	 * 转账操作
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "zZhang")
	@ResponseBody
	public AjaxJson zZhang(HttpServletRequest request) {	
		AjaxJson j = new AjaxJson();
		String uPw = null;		//会员密码  --zyb
		
		//获取会员id  --zyb
		Object mbId = request.getSession().getAttribute("mbId");
		//判断登录是否超时  --zyb
		if( StringUtil.isEmpty(mbId.toString()) ){						
			j.setMsg("请登录！");
			return j;
		}else{
			//积分转账  --zyb
			String yb = request.getParameter("yb");
			String ru = request.getParameter("ru");
			String pw = request.getParameter("pw");
			String zzly = request.getParameter("zzly");
			String lb1 = null;
			String lb2 = null;
			
			
			List<Map<String, Object>> ulb1 = systemService.findForJdbc("SELECT lunbi,passWord FROM bns_users_info WHERE user_id = '"+mbId+"'");
			if(ulb1!=null && ulb1.size()>0){
				if(ulb1.get(0).get("lunbi") != null)
					lb1 = ulb1.get(0).get("lunbi").toString();
					uPw = ulb1.get(0).get("passWord").toString();
			}else{
				ulb1 = null;
			}
			List<Map<String, Object>> ulb2 = systemService.findForJdbc("SELECT lunbi FROM bns_users_info WHERE user_id = '"+ru+"'");
			if(ulb2!=null && ulb2.size()>0){
				if(ulb2.get(0).get("lunbi") != null)
					lb2 = ulb2.get(0).get("lunbi").toString();
			}else{
				j.setMsg("该用户不存在！");
				j.setSuccess(false);
				return j;
			}
			
			if(StringUtil.isNotEmpty(pw)){
				String myPw = EncryptUtil.encrypt(pw, "MD5");
				//密码相同，进行转账操作  --zyb
				if(myPw.equals(uPw)){
					int lbb1 = Integer.valueOf(lb1) - Integer.valueOf(yb);
					if(lbb1 < 10){
						j.setMsg("余额不足！");
						j.setSuccess(false);
						return j;
					}
					int lbb2 = Integer.valueOf(lb2) + Integer.valueOf(yb);
					systemService.updateBySqlString("UPDATE bns_users_info SET lunbi = '"+lbb1+"' WHERE user_id = '"+mbId+"'");	//转账
					//添加积分记录
					BnsIntegralEntity inte1 = new BnsIntegralEntity();	
					inte1.setUserId(Integer.parseInt(String.valueOf(mbId)));
					inte1.setAddType("zzzc");
					inte1.setIntegralType("yb");
					inte1.setAddDate(new Date());
					inte1.setIntegral(- Integer.valueOf(yb));
					inte1.setInOut("zc");
					integralService.save(inte1);
					
					systemService.updateBySqlString("UPDATE bns_users_info SET lunbi = '"+lbb2+"' WHERE user_id = '"+ru+"'");	//转账
					//添加积分记录
					BnsIntegralEntity inte2 = new BnsIntegralEntity();	
					inte2.setUserId(Integer.parseInt(String.valueOf(ru)));
					inte2.setAddType("zzjs");
					inte2.setIntegralType("yb");
					inte2.setAddDate(new Date());
					inte2.setIntegral(Integer.valueOf(yb));
					inte2.setInOut("sr");
					integralService.save(inte2);
					
					j.setMsg("转账成功！");
					j.setSuccess(true);
					return j;
				}else{
					j.setMsg("密码错误！");
					j.setSuccess(false);
					return j;
				}
			}else{
				j.setMsg("请输入密码！");
				j.setSuccess(false);
				return j;
			}
		}
		
		
	}
	
	/**
	 * 兑换操作
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "dHuan")
	@ResponseBody
	public AjaxJson dHuan(HttpServletRequest request) {	
		AjaxJson j = new AjaxJson();
		String uPw = null;		//会员密码  --zyb
		
		//获取会员id  --zyb
		Object mbId = request.getSession().getAttribute("mbId");
		//判断登录是否超时  --zyb
		if( StringUtil.isEmpty(mbId.toString()) ){						
			j.setMsg("请登录！");
			return j;
		}else{
			//兑换  --zyb
			String dh1 = request.getParameter("dh1");
			String dh2 = request.getParameter("dh2");
			String slc1 = request.getParameter("slc1");
			String slc2 = request.getParameter("slc2");
			String dhpw = request.getParameter("dhpw");
			int mySlc1 = 0;
			int mySlc2 = 0;
		
			List<Map<String, Object>> slc2Num = systemService.findForJdbc("SELECT * FROM bns_users_info WHERE user_id = '"+mbId+"'");
			System.out.print("--------------rn:"+slc2Num.get(0).get("release_num"));
			if(slc2Num != null && slc2Num.size() > 0){
				if(slc2Num.get(0).get(slc2) != null && slc2Num.get(0).get(slc1) != null){
					mySlc1 = Integer.parseInt(String.valueOf(slc2Num.get(0).get(slc1)));
					mySlc2 = Integer.parseInt(String.valueOf(slc2Num.get(0).get(slc2)));	//数据库中的数量				
					mySlc2 = mySlc2 - Integer.valueOf(dh2);
					uPw = slc2Num.get(0).get("passWord").toString();
					if(mySlc2 < 10){	//如果余额小于10						
						j.setMsg("余额不足！");
						j.setSuccess(false);
						return j;
					}else{
						mySlc1 = mySlc1 + Integer.valueOf(dh1);
					}
				}else{
					j.setMsg("操作失败！");
					j.setSuccess(false);
					return j;
				}
			}else{
				j.setMsg("操作失败！");
				j.setSuccess(false);
				return j;
			}
			
			
			if(StringUtil.isNotEmpty(dhpw) && StringUtil.isNotEmpty(uPw)){
				String myPw = EncryptUtil.encrypt(dhpw, "MD5");
				//密码相同，进行兑换操作  --zyb
				if(myPw.equals(uPw)){					
					systemService.updateBySqlString("UPDATE bns_users_info SET " +slc1+ "=" +mySlc1+ "," +slc2+ "=" +mySlc2+ " WHERE user_id ="+mbId);
					
					//添加积分记录1
					BnsIntegralEntity dhInte1 = new BnsIntegralEntity();	
					dhInte1.setUserId(Integer.parseInt(String.valueOf(mbId)));
					dhInte1.setAddType("jfdh");	//积分兑换
					if("lunbi".equals(slc1))
						dhInte1.setIntegralType("yb");
					if("jindou".equals(slc1))
						dhInte1.setIntegralType("jd");
					if("cash".equals(slc1))
						dhInte1.setIntegralType("xj");
					dhInte1.setAddDate(new Date());
					dhInte1.setIntegral(Integer.valueOf(dh1));
					dhInte1.setInOut("sr");
					integralService.save(dhInte1);
					//添加积分记录2
					BnsIntegralEntity dhInte2 = new BnsIntegralEntity();	
					dhInte2.setUserId(Integer.parseInt(String.valueOf(mbId)));
					dhInte2.setAddType("jfdhzc");	//积分兑换支出
					if("lunbi".equals(slc2))
						dhInte2.setIntegralType("yb");					
					if("cash".equals(slc2))
						dhInte2.setIntegralType("xj");
					dhInte2.setAddDate(new Date());
					dhInte2.setIntegral(-Integer.valueOf(dh2));
					dhInte2.setInOut("zc");
					integralService.save(dhInte2);
					
					j.setMsg("兑换成功！");
					j.setSuccess(true);
					return j;
				}else{
					j.setMsg("密码错误！");
					j.setSuccess(false);
					return j;
				}
			}else{
				j.setMsg("请输入密码！");
				j.setSuccess(false);
				return j;
			}
		}
	}
	

	
}
